#include "widgetquadri.h"


OpenGlWidget::OpenGlWidget(QWidget *parent)
    : QGLWidget(parent)
{
    setMouseTracking(true);
    left_pressed = false;
    zoom_cam = 100.0;
    angle_x = angle_y = angle_z = 0;
    loaderOBJ loader("Quadrirotor.obj");
    loader.loadObject(_objets);
}

/****       Keyboard events       ****/

void OpenGlWidget::keyPressEvent(QKeyEvent *keyEvent)
{
    switch(keyEvent->key())
    {
        case Qt::Key_Escape:
            this->close();
            emit close_app(true);
            break;
        case Qt::Key_R:
            angle_x = angle_y = angle_z = 0;
            emit sendAngleX((float)angle_x);
            updateGL();
            break;
    }
}

/****       Mouse events       ****/

void OpenGlWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
        left_pressed = true;
}
void OpenGlWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
        left_pressed = false;
}

void OpenGlWidget::mouseMoveEvent(QMouseEvent *mouseEvent)
{
    if(left_pressed && old_point != mouseEvent->pos())
    {
        if(old_point.x() - mouseEvent->x() > 0)
            angle_y -= angle_pas;
        else if(old_point.x() - mouseEvent->x() < 0)
            angle_y += angle_pas;
        if(old_point.y() - mouseEvent->y() > 0)
            angle_z += angle_pas;
        else if(old_point.y() - mouseEvent->y() < 0)
            angle_z -= angle_pas;
        old_point = mouseEvent->pos();
        emit sendAngleX((float)angle_x);
    }
    updateGL();
}

void OpenGlWidget::wheelEvent(QWheelEvent *event)
{
    float num_degree;
    if(event->orientation() == Qt::Vertical)
        num_degree = event->delta() / 8;
        if(num_degree > 0)
            zoom_cam -= zoom_pas;
        else if(num_degree < 0)
            zoom_cam += zoom_pas;
    updateGL();
}

/****       Paint Opengl       ****/

void OpenGlWidget::initializeGL()
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void OpenGlWidget::resizeGL(int width, int height)
{
    if(height == 0)
        height = 1;
    SIZEX=width;
    SIZEY=height;
    glViewport(0, 0, width, height);
}

void OpenGlWidget::paintGL()
{

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(zoom_cam, SIZEX/(float)SIZEY, 1.0, 1000.);
    gluLookAt(5., 5., 0., 0., 0., 0., 0., 1., 0.);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //glRotatef(angle_x, 1., 0., 0.);
    glRotatef(angle_y, 0., 1., 0.);
    glRotatef(angle_z, 0., 0., 1.);

    for(int i = 0 ; i < _objets.size(); i++)
    {
        glBegin(GL_QUADS);
        for(int j = 0 ; j < _objets.at(i).getNbPoint(); j++)
        {
            glColor3d(_objets.at(i).at(j).getR(),
                      _objets.at(i).at(j).getG(),
                      _objets.at(i).at(j).getB());
            glVertex3f(_objets.at(i).at(j).getX(),
                       _objets.at(i).at(j).getY(),
                       _objets.at(i).at(j).getZ());
        }
        glEnd();
    }

    glLoadIdentity();

//    glRotatef(angle_x, 1., 0., 0.);
//    glRotatef(angle_y, 0., 1., 0.);
//    glRotatef(angle_z, 0., 0., 1.);
    glTranslatef(0.f, 0.5f, 0.f);

    glBegin(GL_LINES);
        // Red line X
        glColor3d(1, 0, 0);
        glVertex3i(0,0,0);
        glVertex3i(1,0,0);

        // Green line Y
        glColor3d(0, 1, 0);
        glVertex3i(0,0,0);
        glVertex3i(0,1,0);

        // Blue line Z
        glColor3d(0, 0, 1);
        glVertex3i(0,0,0);
        glVertex3i(0,0,1);
    glEnd();
}

void OpenGlWidget::testSlots(QPoint p)
{
    if(p.x() > 10)
        angle_z = 0;
}
